/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package jhelper;

import JhelperModels.AddressBookBean;
import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.Panel;
import javax.swing.JPanel;
import java.awt.Graphics;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import java.util.*;
import jhelper.blls.AddressBook;
import jhelper.models.AddressBooksBean;

/**
 *
 * @author Administrator
 */

   public class AddressBookPanel extends JPanel{
    private final int FlowLayout;

    private static class AddressBooks {

        public AddressBooks() {
        }

        private 从AddressBooks获得所有通讯录信息 getAllCards() {
            throw new UnsupportedOperationException("Not yet implemented");
        }

        private boolean delCard(String name) {
            throw new UnsupportedOperationException("Not yet implemented");
        }
    }

    private static class 从AddressBooks获得所有通讯录信息 {

        public 从AddressBooks获得所有通讯录信息() {
        }

        private Enumeration keys() {
            throw new UnsupportedOperationException("Not yet implemented");
        }
    }
      //声明面板中的组件
Panel p,pleft,pleft_main,pleft_down;
JPanel pQuery;
JScrollPane sp;
JList lall;
DefaultListModel listmode;//列表框的数据模型对象
JButton btnQuery,btnAdd,btnSave,btnDel;
JTextField txtName,txtOffice,txtHome,txtMobile,txtQQ,txtEmail,txtOther,txtCondition;
JLabel lName,lOffice,lHome,lMobile,lQQ,lEmail,lOther,lMsg;
//定义一个Hashtable的集合用来存储所有通讯录的信息
Hashtable h;
//初始化界面
public AddressBookPanel(){
//初始化组件
p=new Panel();
pleft=new Panel();
pleft_main=new Panel();
pQuery=new JPanel();
btnQuery=new JButton("查询");
btnSave=new JButton("保存");
btnDel=new JButton("删除");
btnAdd=new JButton("添加");
txtName=new JTextField("",20);
txtName.setEnabled(false);//设置文本框的编辑状态为锁定
txtOffice=new JTextField("",20);
txtHome=new JTextField("",20);
txtMobile=new JTextField("",20);
txtQQ=new JTextField("",20);
txtEmail=new JTextField("",20);
txtOther=new JTextField("",20);
txtCondition=new JTextField("",20);
lName=new JLabel("姓名",JLabel.CENTER);
lOffice=new JLabel("办公电话",JLabel.CENTER);
lHome=new JLabel("家庭电话",JLabel.CENTER);
lMobile=new JLabel("手机",JLabel.CENTER);
lQQ=new JLabel("QQ",JLabel.CENTER);
lEmail=new JLabel("Email",JLabel.CENTER);
lOther=new JLabel("其他",JLabel.CENTER);
lMsg=new JLabel("请输入要查询的联系人姓名",JLabel.CENTER);
//定义一个带滚动轴的面板，用来放置列表框
sp=new JScrollPane();
从AddressBooks获得所有通讯录信息
h=new AddressBooks().getAllCards();
listmode=new DefaultListModel();
if(h!=null){
   listmode.addElement("点击如下名单可查看相关信息");
   //获得所有通讯录的关键字—姓名，以枚举的形式返回个姓名的集合
   Enumeration e=h.keys();
   while(e.hasMoreElement()){
        listmode.addElement(e.nextElement());
}else{
  listmode.addElement("通讯录为空");
  txtName.setEnabled(true);
}
//实例化列表框，把数据"挂"上去
lall=new JList(listmode);
//将列表框放置到滚动面板上
sp.setViewportView(lall);
//面板管理
pQuery.setBorder(BorderFactory.createTitledBorder("联系人查询"));
pQuery.setLayout(new FlowLayout(FlowLayout.CENTER));
pQuery.add(lMsg);
pQuery.add(txtCondition);
pQuery.add(btnQuery);
pleft_main.setLayout(new GridLayout(7,2));
pleft_main.add(lName);
pleft_main.add(txtName);
pleft_main.add(lMobile);
pleft_main.add(txtMobile);
pleft_main.add(lOffice);
pleft_main.add(txtOffice);
pleft_main.add(lHome);
pleft_main.add(txtHome);
pleft_main.add(lQQ);
pleft_main.add(txtQQ);
pleft_main.add(lEmail);
pleft_main.add(txtEmail);
pleft_main.add(lOther);
pleft_main.add(txtOther);
//定义一个中间面板，用来放表格布局的pleft_main
p.setLayout(new FlowLayout(FlowLayout.CENTER));
p.add(pleft_main);
pleft_down.setLayout(new FlowLayout(FlowLayout.CENTER));
pleft_down.add(pQuery);
pleft_down.add(btnAdd);
pleft_down.add(btnSave);
pleft_down.add(btnDel);
pleft.setLayout(new BorderLayout());
pleft.add("South",pleft_down);
this.setLayout(new BorderLayout());
this.add("East",sp);
this.add("Center",pleft);
//列表中第二项数据默认是选中状态
if（h!=null){
    lall.setSelectedIndex(1);
    showCard(lall.getSelectedValue().toString());
}
//为列表框添加时间监听，当用户单击列表框上的某个姓名；
              //左边的窗体里面显示具体的信息
lall.addListSelectionListener(new ListSelectionListener(){
  public void valueChanged(ListSelectionEvent e){
//得到用户选择的列表项目
int index=lall.getSelectedIndex();
Object name=lall.getSelectedValue();
if(index>0){
//调用自定义的函数showCard,用来显示指定姓名的名片信息
showCard(name.toString());
      }
   }
});
//为添加按钮添加单击事件监听，当用户点击按钮后，所有文本框清空，等待用户输入新的卡片信息
btnAdd.addActionListener(new ActionListener(){
  public void actionPerformed(ActionEvent e){
    //调用用户自定义的函数ClearText，用来清空文本框
    clearText();
   }
});
//为保存按钮添加单击事件监听，当用户点击按钮后，
//对用户添加和修改的数据进行保存
  btnSave.addActionListener(new ActionListener(){
     public void actionPerformed(ActionEvent e){
       //根据姓名文本框的状态判断是新增名片还是修改名片
         if(txtName.isEnabled()==true){
            addNew();
}else{
            update();
     }
     //更新列表框信息
     refleshList();
       }

            private void refleshList() {
                throw new UnsupportedOperationException("Not yet implemented");
            }
 });
//为删除按钮添加单击事件监听，当用户点击按钮后，删除指定卡片
btnDel.addActionListener(new ActionListener(){
    public void actionPerformed(ActionEvent e){
       //根据姓名文本框的状态判断是新增名片还是修改名片
       delete();
//更新列表框信息
refreshList();
      }

            private void refreshList() {
                throw new UnsupportedOperationException("Not yet implemented");
            }
        });
//为查询按钮添加单击事件监听，当用户点击按钮后，查询指定的卡片
btnQuery.addActionListener(new ActionListener(){
   public void actionPerformed(ActionEvent e){
       String name=txtCondition.getText();
        showCard(name);
     }
   });
}
public void delete(){
    if(txtName.getText().trim().length()==0){
       JOptionPane.showMessageDialog(null,"必须输入联系人姓名");
       return;
}
//获得用户输入的信息
String name=txtName.getText();
txtName.setEnabled(false);
if(new AddressBooks().delCard(name)==true){
     JOptionPane.showMessageDialog(null,"成功删除了"+name+"的联系信息");
   }else{
     JOptionPane.showMessageDialog(null,"联系人"+name+"的信息删除失败");
    }
}
public void refrshList(){
    //从AddressBooks获得所有通讯录信息
h=new AddressBooks().getAllCards();
if(h!=null){
      listmode.clear();
      listmode.addElement("点击如下名单可查看相关信息");
      //获得所有通讯录的关键字——姓名，以枚举的形式返回这个姓名的集合
         Enumeration e=h.keys();
         while(e.hasModeElements()){
            listmode.addElement(e.nextElement());
          }
}else{
     listmode.clear();//删除原有的数据
     listmode.addElement("通讯录为空");


      }
}

public void showCard(String name){
Object temp=h.get(name);
if(temp==null){
    JOptionPane.showMessageDialog(null,"没有找到联系人:"+name,"提示",JOptionPane.ERROR_MESSAGE);
    return;
}
AddressBookBean b=(AddressBookBean)temp;
txtName.setText(b.getName());
txtName.setEditable(false);
txtMobile.setText(b.getMobile());
txtOffice.setText(b.getOffice());
txtHome.setText(b.getHome());
txtEmail.setText(b.getEmail());
txtOther.setText(b.getOther());
txtQQ.setText(b.getQq());

} 
   
public void clearText(){
txtName.setText("");
txtName.setEnabled(true);
txtName.setEditable(true);
txtMobile.setText("");
txtOffice.setText("");
txtHome.setText("");
txtEmail.setText("");
txtOther.setText("");
txtQQ.setText("");

} 

public void addNew(){
 if(txtName.getText().trim().length()==0) {
   JOptionPane.showMessageDialog(null,"必须输入联系人姓名");  
    return;
 }  
  String name=txtName.getText();
  String mobile=txtMobile.getText();
  mobile=mobile.trim().length()==0?"空":mobile.trim();
  String office=txtOffice.getText();
  office=office.trim().length()==0?"空":office.trim();
   String home=txtHome.getText();
   home=home.trim().length()==0?"空":home.trim();
   String qq=txtQQ.getText();
   qq=qq.trim().length()==0?"空":qq.trim();
   String other=txtOther.getText();
   other=other.trim().length()==0?"空":other.trim();
   String email=txtEmail.getText();
  email=email.trim().length()==0?"空":email.trim();
   AddressBookBean newcard=new AddressBookBean(name,mobile,office,home,qq,email,other);
   if(new AddressBook().newCard(newcard)==true){
    JOptionPane.showMessageDialog(null,"成功添加了"+name+"的联系信息");     
       
   }else{
        JOptionPane.showMessageDialog(null,"联系人"+name+"的联系信息添加失败");  
   }
  txtName.setEnabled(false);
}
public void update(){
    
     if(txtName.getText().trim().length()==0) {
   JOptionPane.showMessageDialog(null,"必须输入联系人姓名");  
    return;
 }  
    
    String name=txtName.getText();
  String mobile=txtMobile.getText();
  mobile=mobile.trim().length()==0?"空":mobile.trim();
  String office=txtOffice.getText();
  office=office.trim().length()==0?"空":office.trim();
   String home=txtHome.getText();
   home=home.trim().length()==0?"空":home.trim();
   String qq=txtQQ.getText();
   qq=qq.trim().length()==0?"空":qq.trim();
   String other=txtOther.getText();
   other=other.trim().length()==0?"空":other.trim();
   String email=txtEmail.getText();
  email=email.trim().length()==0?"空":email.trim();
    AddressBookBean oldcard=new AddressBookBean(name,mobile,office,home,qq,email,other);
    
     if(new AddressBook().newCard(oldcard)==true){
    JOptionPane.showMessageDialog(null,"成功修改了"+name+"的联系信息");     
       
   }else{
        JOptionPane.showMessageDialog(null,"联系人"+name+"的联系信息修改失败");  
   }

}
public void paintComponent(Graphics g){
    super.paintComponents(g);
}
    public static void main(String[] args) {
        Frame f=new Frame();
        f.add(new AddresBookPanel());
        f.pack();
        f.show();
        f.addWindowListener(new WindowAdapter(){
           public void windowClosing(WindowEvent e) {
               System.exit(0);
           }
            
        });
    }
}
